package com.yanqu.road.server.cmd.orewar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.crossorewar.CrossUserOrePit;
import com.yanqu.road.entity.enums.activity.eActivityOreWarType;
import com.yanqu.road.entity.orewar.UserOrePit;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.orewar.OreWarProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.orewar.OreWarActivityMgr;
import com.yanqu.road.server.manger.activity.orewar.OreWarMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.pb.OreWarPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.S_ORE_WAR_STATION_DETAIL, desc = "矿站详细信息")
public class OreStationDetailCmd extends AbstractCommand {

	private int activityId;

	private int startPitId;

	private int endPitId;

	private List<UserOrePit> pitList = new ArrayList<>();
	private List<CrossUserOrePit> crossPitList = new ArrayList<>();

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		OreWarProto.OreGetStationDetailReqMsg reqMsg = OreWarProto.OreGetStationDetailReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		startPitId = reqMsg.getStartPitId();
		endPitId = reqMsg.getEndPitId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		ActivityInfo activityInfo = OreWarActivityMgr.getOpenActivityInfo(activityId);
		if(activityInfo != null){
			if(activityInfo.getChildType() == eActivityOreWarType.OreWar.getChildType()){
				pitList = OreWarMgr.getUserOrePitList(activityId, startPitId, endPitId);
			}else if(activityInfo.getChildType() == eActivityOreWarType.CrossOreWar.getChildType()){
				OreWarProto.CrossOreGetStationDetailReqMsg.Builder msg = OreWarProto.CrossOreGetStationDetailReqMsg.newBuilder();
				msg.setActivityId(activityId);
				msg.setStartPitId(startPitId);
				msg.setEndPitId(endPitId);
				int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId());
				GamePlayerMgr.sendUnionActivityGroupPacket(Protocol.C_CROSS_ORE_WAR_PIT_LIST,msg,player.getUserId(),activityId,groupId);

			}
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		ActivityInfo activityInfo = OreWarActivityMgr.getOpenActivityInfo(activityId);
		if(activityInfo != null && activityInfo.getChildType() == eActivityOreWarType.OreWar.getChildType()){
			OreWarProto.OrePitListMsg.Builder respMsg = OreWarPb.parseOrePitListMsg(pitList);
			player.sendPacket(Protocol.U_ORE_WAR_STATION_DETAIL, respMsg);
		}
	}
}
